package xyz.xtt.admin.comAnnotation.impl;

/**
 * @author xugaopeng
 * @date 2024-12-16-10:37
 */

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.stereotype.Component;
import xyz.xtt.admin.comAnnotation.EqualsValues;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@Component
public class EqualsValueValidator implements ConstraintValidator<EqualsValues, Object> {

    private Set<String> allowedValues;

    @Override
    public void initialize(EqualsValues constraintAnnotation) {
        this.allowedValues = new HashSet<>(Arrays.asList(constraintAnnotation.anyof()));
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null) {
            return false; // 根据业务需求决定null是否合法
        }
        return allowedValues.contains(value.toString());
    }
}